var bg_run = document.getElementById("bg-run");
var show_msg = document.getElementById("show-msg");

function init(){
	chrome.storage.local.get(function(obj){
		if(isNull(obj)){
			
		}else{
			if(obj.bg_run==false){
				bg_run.selectedIndex = 1;
			}else{
				bg_run.selectedIndex = 0;
			}

			if(obj.show_msg == false){
				show_msg.selectedIndex = 1;
			}else{
				show_msg.selectedIndex = 0;
			}
		}
	});
}

function isNull(obj) {
	for(var name in obj){
		return false;
	}
	return true;
}

bg_run.addEventListener("change",function(){
	if(bg_run.value == "Yes"){
		//true
		chrome.storage.local.set({"bg_run":true});
		chrome.runtime.getBackgroundPage(function(view){
			view.connectWS();
		});
	}else{
		//false
		chrome.storage.local.set({"bg_run":false});
		chrome.runtime.getBackgroundPage(function(view){
			view.closeWS();
		});
	}
	saveSetting();
});

show_msg.addEventListener("change",function(){
	if(show_msg.value == "Yes"){
		//true
		chrome.storage.local.set({"show_msg":true});
	}else{
		//false
		chrome.storage.local.set({"show_msg":false});
	}
	saveSetting();
});

function saveSetting(){
	var views = chrome.extension.getViews();
	  for (var i = 0; i < views.length; i++) {
	    var view = views[i];
	    if (view.location.href.endsWith("background.html")) {
	      view.init();
	      break; // 完成
	    }
	  }
}

init();